package com.rst.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Exam;
import com.rst.model.Section;

@Repository
@Transactional
public class ExamDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public void saveExam(Exam exam) {
		sessionFactory.getCurrentSession().save(exam);	
		
	}

	public Exam getExamById(Long examId) {
	    Session session = sessionFactory.getCurrentSession();
	    Exam exam = (Exam)session.get(Exam.class, examId);
		return exam;
	}

	@SuppressWarnings("unchecked")
	public List<Exam> getExamList() {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Exam e WHERE e.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Exam.ACTIVE);
	    
	    List<Exam> examList = query.list(); 
	    for (Exam exam : examList) {
	    	 Hibernate.initialize(exam.getSubject());
		}
		return query.list();
	}

	public void updateExam(Exam exam) {
		sessionFactory.getCurrentSession().update(exam);		
	}
}




